/*
' Copyright (c) 2011  DotNetNuke Corporation
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Security;
using DotNetNuke.Modules.DNNFiddle.Data;
using DotNetNuke.Web.Client.ClientResourceManagement;
using System.Web.UI.WebControls;
using System.Web.UI;
using DotNetNuke.Framework;
using System.Collections.Generic;
using ClientDependency.Core.FileRegistration.Providers;


namespace DotNetNuke.Modules.DNNFiddle
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The ViewDNNFiddle class displays the content
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class View : PortalModuleBase, IActionable
    {
        private const string CssEmbedWithCode = "<style type=\"text/css\" >{0}</style>";
        private const string CssEmbedWithSource = "<link href=\"{0}\" />";
        private const string ScriptEmbedWithCode = "<script type=\"text/javascript\">{0}</script>";
        private const string ScriptEmbedWithSource = "<script type=\"text/javascript\" src=\"{0}\" ></script>";

        #region Event Handlers

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

        private void InjectObject(string key, string content)
        {
            var loader = Page.FindControl(key);
            if (loader != null)
            {
                loader.Controls.Add(new LiteralControl(content));
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                FiddleViewModel fvm = FiddleDataService.Instance.GetFiddleDTO(this.ModuleId);
                if (fvm != null)
                {
                    this.content.InnerHtml = fvm.html;

                    var css = String.Format(CssEmbedWithCode, fvm.css);
                    var script = String.Format(ScriptEmbedWithCode, fvm.script);

                    InjectObject("SCRIPTS", css);
                    InjectObject("ClientResourcesFormBottom", script);

                    foreach (ScriptViewModel scriptItem in fvm.scripts)
                    {
                        if (scriptItem.enabled)
                        {
                            switch (scriptItem.name)
                            {
                                case "jQuery":
                                    jQuery.RequestRegistration();
                                    break;
                                case "jQuery UI":
                                    jQuery.RequestUIRegistration();
                                    break;
                                default:
                                    var scriptLink = string.Format(ScriptEmbedWithSource, scriptItem.url);
                                    InjectObject("BodySCRIPTS", scriptLink);
                                    break;
                            }
                        }
                    } 
                }

            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

        #region Optional Interfaces

        public ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();

                Actions.Add(GetNextActionID(), Localization.GetString("EditModule", this.LocalResourceFile), "", "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false);

                return Actions;
            }
        }

        #endregion

    }

}
